<?php
namespace app\admin\controller;
use app\admin\logic\ClassTiLogic;

/**
 * 题目api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/22
 * Time: 15:08
 */
class ClassTi extends Base {

    public function __construct(){
        parent::__construct();
        $this->ct_logic = new ClassTiLogic();
    }

    /**
     * 获取题目
     * @return \think\response\Json
     */
    public function ajax_get_ct(){
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $maps = $this->ct_logic->get_search_ct_maps($params['ckid'],$params['question']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('class_ti')->get_list($maps,'id,question,option,answer,cailiao','id desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            $list[$k]['option'] = !empty($v['option']) ? json_decode($v['option'],true) : [];
        }

        $total_num = model('class_ti')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return json($this->ct_logic->get_response(1,'OK',$data));
    }

    /**
     * 获取题目详情
     * @return \think\response\Json
     */
    public function ajax_get_ct_detail(){
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $maps = [];
        $maps[] = ['id','=',$params['ctid']];
        $detail = model('class_ti')->get_detail($maps,'id,question,option,answer,cailiao');

        $detail['option'] = !empty($detail['option']) ? json_decode($detail['option'],true) : [];

        $data['detail'] = $detail;
        return json($this->ct_logic->get_response(1,'OK',$data));
    }

    /**
     * 新增题目
     * @return \think\response\Json
     */
    public function ajax_add_ct(){
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //检查新增题目规则
        $r = $this->ct_logic->check_add_ct_rule($params);
        if(!$r['code']){
            return json($r);
        }
        $ck_detail = $r['data']['ck_detail'];

        $ct_data = [];
        $ct_data['ckid'] = $ck_detail['id'];
        $ct_data['question'] = trim($params['question']);
        $ct_data['option'] = json_encode($params['option'],JSON_UNESCAPED_UNICODE);
        $ct_data['answer'] = trim($params['answer']);
        //$ct_data['cailiao'] = trim($params['cailiao']);
        $r = model('class_ti')->insert($ct_data);
        if($r){
            return json($this->ct_logic->get_response(1,'新增成功'));
        }
        return json($this->ct_logic->get_response(0,'新增失败'));
    }

    /**
     * 修改题目
     * @return \think\response\Json
     */
    public function ajax_save_ct(){
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //检查修改题目规则
        $r = $this->ct_logic->check_save_ct_rule($params);
        if(!$r['code']){
            return json($r);
        }
        $ct_detail = $r['data']['ct_detail'];

        $ct_data = [];
        $ct_data['question'] = trim($params['question']);
        $ct_data['option'] = json_encode($params['option'],JSON_UNESCAPED_UNICODE);
        $ct_data['answer'] = trim($params['answer']);
        //$ct_data['cailiao'] = trim($params['cailiao']);

        $maps = [];
        $maps[] = ['id','=',$ct_detail['id']];
        $r = model('class_ti')->force()->save($ct_data,$maps);
        if($r !== false){
            return json($this->ct_logic->get_response(1,'修改成功'));
        }
        return json($this->ct_logic->get_response(0,'修改失败'));
    }
}